﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chamberlain.BL;
using Microsoft.Office.Interop.Excel;
using System.Reflection; 

namespace Chamberlain
{
    public partial class Busquedas : Form
    {
        public Busquedas()
        {
            InitializeComponent();

            iniciarTodo();
        }

        private void iniciarTodo()
        {

            rb1.Checked = true;
            rbVerano.Checked = true;

            rellenaCombos();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Horario ho = (Horario)cbHorario.SelectedItem;
            CanchaxHorario ch = new CanchaxHorario();
            ch.OHorario = ho;

            List<CanchaxHorario> lch = new List<CanchaxHorario>();
            lch = ch.readall();
            List<Cancha> lc = new List<Cancha>();
            foreach (CanchaxHorario cho in lch)
            {
                lc.Add(cho.OCancha);
            }

            cbCancha.DataSource = lc;
        }



        private bool validacontroles(int tipo)
        {
            bool aux = true;
            if (tipo == 1)
            {
                if (txtNombre.Text != "" || txtApellidoPaterno.Text != "" || txtApellidoMaterno.Text != "")
                    aux = true;
                else
                {
                    aux = false;
                    MessageBox.Show("Profavor, ingrese un nombre a buscar");
                }
            }

            return aux;

        }


        private void rellenaCombosBusquedas()
        {
            Cancha ch = new Cancha();
            List<Cancha> lch = new List<Cancha>();
            lch = ch.readall();
            cbCancha.DataSource = lch;

        }



        private void button1_Click(object sender, EventArgs e)
        {
            dgvAlumno.DataSource = null;

            int aux;
            if (rb1.Checked == true)
                aux = 1;
            else if (rb2.Checked == true)
                aux = 2;
            else aux = 3;


            Alumno alux = new Alumno();
            CMatricula mat = new CMatricula();
            List<Alumno> la = new List<Alumno>();
            string auxTemp;
            switch (aux)
            {
                case 1: if (validacontroles(1) == true)
                    {
                        alux.Nombre = txtNombre.Text;
                        alux.ApellidoPaterno = txtApellidoPaterno.Text;
                        alux.ApellidoMaterno = txtApellidoMaterno.Text;
                        la = alux.findxNombre();
                    }
                    break;
                case 2: 
                    alux.Sexo='C';
                    if (rbMasculino.Checked == true)
                        alux.Sexo = 'M';
                    else if (rbFemenino.Checked == true)
                        alux.Sexo = 'F';
                    la = alux.findxEdad(int.Parse(numEdadMin.Value.ToString()), int.Parse(numEdadMax.Value.ToString()));
                    break;
                case 3: if (rbVerano.Checked == true)
                        auxTemp = "verano";
                    else auxTemp = "invierno";
                         mat.OModulo = new Modulo();
                         mat.OModulo.Temporada = auxTemp;
                         mat.OModulo.OCanchaxHorario = new CanchaxHorario();
                         mat.OModulo.OCanchaxHorario.OCancha = new Cancha();
                         mat.OModulo.OCanchaxHorario.OCancha = (Cancha)cbCancha.SelectedItem;
                         mat.OModulo.OCanchaxHorario.OHorario = new Horario();
                         mat.OModulo.OCanchaxHorario.OHorario = (Horario)cbHorario.SelectedItem;
                         mat.OModulo.readbyOCH();
                         la = mat.findxModulo();
                    break;
            }
            if (la.Count != 0)
                dgvAlumno.DataSource = la;
            else MessageBox.Show("Su busqueda no ha arrojado resultados.");


        }

        private void btn_EditarInfo_Click(object sender, EventArgs e)
        {
            if (dgvAlumno.SelectedRows.Count == 1)
            {
                Alumno allll = new Alumno();
                allll.Id = (int)dgvAlumno.CurrentCell.Value;
                allll.read();
                FrmAlumno fa = new FrmAlumno(allll);
                fa.Show();

            }
            else
                MessageBox.Show("Debe seleccionar solo un Alumno para continuar.");


        }



        private void panel3_MouseClick(object sender, MouseEventArgs e)
        {
            rb1.Checked = false;
            rb2.Checked = true;
            rb3.Checked = false;
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            rb1.Checked = true;
            rb2.Checked = false;
            rb3.Checked = false;
        }

        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            rb1.Checked = false;
            rb2.Checked = false;
            rb3.Checked = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvAlumno.SelectedRows.Count == 1)
            {
                Alumno allll = new Alumno();
                allll.Id = (int)dgvAlumno.CurrentCell.Value;
                allll.read();
                Matricula a = new Matricula(allll, 1);
                a.Show();

            }
            else
                MessageBox.Show("Debe seleccionar solo un Alumno para continuar.");

        }

        private void btn_Matricular_Click(object sender, EventArgs e)
        {
            if (dgvAlumno.SelectedRows.Count == 1)
            {
                Alumno allll = new Alumno();
                allll.Id = (int)dgvAlumno.CurrentCell.Value;
                allll.read();
                Matricula a = new Matricula(allll, 2);
                a.Show();

            }
            else
                MessageBox.Show("Debe seleccionar solo un Alumno para continuar.");
        }

        private void BtnImprimir_Click(object sender, EventArgs e)
        {
        
                Microsoft.Office.Interop.Excel.Application oExcel_12 = null;                //Excel_12 Application

                Microsoft.Office.Interop.Excel.Workbook oBook = null;                       // Excel_12 Workbook

                Microsoft.Office.Interop.Excel.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection

                Microsoft.Office.Interop.Excel.Worksheet oSheet = null;                     // Excel_12 Worksheet

                Microsoft.Office.Interop.Excel.Range oRange = null;                         // Cell or Range in worksheet

                Object oMissing = System.Reflection.Missing.Value;



                // Create an instance of Excel_12.

                oExcel_12 = new Microsoft.Office.Interop.Excel.Application();



                // Make Excel_12 visible to the user.

                oExcel_12.Visible = true;



                // Set the UserControl property so Excel_12 won't shut down.

                oExcel_12.UserControl = true;



                // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");



                // Add a workbook.

                oBook = oExcel_12.Workbooks.Add(oMissing);



                // Get worksheets collection 

                oSheetsColl = oExcel_12.Worksheets;



                // Get Worksheet "Sheet1"

                oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheetsColl.get_Item("Hoja1");



                // Export titles

                for (int j = 0; j < dgvAlumno.Columns.Count; j++)
                {
                    if (j <= 17)
                    {

                        oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j + 1];

                        oRange.Value2 = dgvAlumno.Columns[j].HeaderText;
                    }

                }



                // Export data

                for (int i = 0; i < dgvAlumno.Rows.Count; i++)
                {

                    for (int j = 0; j < dgvAlumno.Columns.Count; j++)
                    {
                        if (j <= 17)
                        {
                            oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i + 2, j + 1];

                            oRange.Value2 = dgvAlumno[j, i].Value;
                        }
                    }

                }



                // Release the variables.

                //oBook.Close(false, oMissing, oMissing);

                oBook = null;



                //oExcel_12.Quit();

                oExcel_12 = null;



                // Collect garbage.

                GC.Collect();



        }

        private void txtEdad_ValueChanged(object sender, EventArgs e)
        {
            rellenaCombos();
        }


        private void rellenaCombos()
        {
            int edad = int.Parse(txtEdad.Value.ToString());
            Modulo m = new Modulo();
            List<Modulo> lm = new List<Modulo>();

            if (rbVerano.Checked == true)
                m.Temporada = "verano";
            else m.Temporada = "invierno";

            lm = m.readall(edad);
            List<Horario> lm2 = new List<Horario>();
            bool aux = false;
            foreach (Modulo mod in lm)
            {
                Horario hoo = mod.OCanchaxHorario.OHorario;
                foreach (Horario hor in lm2)
                {
                    if (hor.Id == hoo.Id)
                    {
                        aux = true;
                        break;
                    }
                }
                if (aux == false)
                    lm2.Add(hoo);
                else aux = false;

            }
            if (lm2 != null)
            {
                cbCancha.DataSource = null;
                
                cbHorario.DataSource = lm2;
                if (lm2.Count == 0)
                {
                    cbHorario.Text = "Busqueda por Horario";
                    cbCancha.Text = "Busqueda por Cancha";
                }
            }

        }

        private void rbVerano_CheckedChanged(object sender, EventArgs e)
        {
            rellenaCombos();
        }

        private void rbInvierno_CheckedChanged(object sender, EventArgs e)
        {
            rellenaCombos();
        }

    }

}
